home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / cp2dekit / samples / binfarc.cpp < prev    next >
C/C++ Source or Header  |  1996-12-29  |  1KB  |  72 lines

  1. //***************************************************************************
  2. //
  3. // this file is (c) '94-'96 Niklas Beisert
  4. //
  5. // this file is part of the cubic player development kit.
  6. // you may only use/modify/spread this file under the terms stated
  7. // in the cubic player development kit accompanying documentation.
  8. //
  9. //***************************************************************************
  10.  
  11.  
  12. #include "binfile.h"
  13.  
  14. abinfile::abinfile()
  15. {
  16. }
  17.  
  18. int abinfile::open(binfile &fil, long ofs, long len)
  19. {
  20.   close();
  21.   int fmode=fil.getmode()&~canchsize;
  22.   if (!fmode||!(fmode&canseek))
  23.     return 0;
  24.   f=&fil;
  25.   fofs=ofs;
  26.   filelen=len;
  27.   filepos=0;
  28.   long l=f->length();
  29.   if (fofs>l)
  30.     fofs=l;
  31.   if ((fofs+filelen)>l)
  32.     filelen=l-fofs;
  33.   mode=fmode;
  34.   return 1;
  35. }
  36.  
  37. long abinfile::read(void *buf, long len)
  38. {
  39.   if (!(mode&canread))
  40.     return 0;
  41.   if ((filepos+len)>filelen)
  42.     len=filelen-filepos;
  43.   f->seek(fofs+filepos);
  44.   len=f->read(buf, len);
  45.   filepos+=len;
  46.   return len;
  47. }
  48.  
  49. long abinfile::write(const void *buf, long len)
  50. {
  51.   if (!(mode&canwrite))
  52.     return 0;
  53.   if ((filepos+len)>filelen)
  54.     len=filelen-filepos;
  55.   f->seek(fofs+filepos);
  56.   len=f->write(buf, len);
  57.   filepos+=len;
  58.   return len;
  59. }
  60.  
  61. long abinfile::seek(long pos)
  62. {
  63.   if (!(mode&canseek))
  64.     return filepos;
  65.   if (pos<0)
  66.     pos=0;
  67.   if (pos>filelen)
  68.     pos=filelen;
  69.   filepos=pos;
  70.   return filepos;
  71. }
  72.